home *** CD-ROM | disk | FTP | other *** search
/ MacFormat España 15 / macformat_15.iso / Shareware Internet / Desarrolladores / gray image 2.1 / display.cc < prev    next >
Text File  |  1995-06-13  |  2KB  |  68 lines

  1. /*
  2.  ***********************************************************************
  3.  *
  4.  *
  5.  *                               Grayscale Image
  6.  *                     Display an image on Macintosh
  7.  *
  8.  * The program converts an image to a pixmap in an offscreen Grafport
  9.  * and has it displayed in a window
  10.  *
  11.  * We take advantage of the fact the image is traversed row-by-row
  12.  * by a PixelPrimAction iterator
  13.  *
  14.  ***********************************************************************
  15.  */
  16.  
  17. #include "image.h"
  18. #include "window.h"
  19.  
  20. void IMAGE::display(const char * title) const
  21. {
  22.   class ImageWindow : public OffScreenWindow
  23.   {
  24.   public:
  25.     ImageWindow(IMAGE& image, const char * title)
  26.     : OffScreenWindow((ScreenRect)image,title,40)    // 40 is Gray CLUT id
  27.     {
  28.                         // Move pixels from IMAGE to a off-screen pixmap
  29.                         // Note that the rows of pixmap may be padded, by
  30.                         // as much as off_to_next_row bytes. So we need
  31.                         // to skip over the padding as we move pixels row
  32.                         // by row
  33.       class ImageToPixMap : public PixelPrimAction
  34.       {
  35.         PixMapHandle pixmap;
  36.         char * pixp;
  37.         const card height, width, off_to_next_row;
  38.         card  curr_row, curr_col;
  39.         void operation(GRAY& pixel)        // put a pixel to pixmap[row,col]
  40.         {
  41.           *pixp++ = pixel;
  42.           if( ++curr_col >= width )
  43.             curr_col = 0, curr_row++, pixp += off_to_next_row;
  44.         }
  45.         public:
  46.         ImageToPixMap(OffScreenWindow& window)
  47.         : pixmap(window.get_pixmap()),
  48.           height(window.height()),
  49.           width(window.width()),
  50.           off_to_next_row(window.bytes_per_row()-window.width()),
  51.           curr_row(0), curr_col(0)
  52.         {
  53.           assert( LockPixels(pixmap) );
  54.           pixp = GetPixBaseAddr(pixmap);
  55.         }
  56.         ~ImageToPixMap(void)
  57.         { 
  58.           assert( curr_row == height && curr_col == 0 );
  59.           UnlockPixels(pixmap); pixmap = 0;
  60.          }
  61.       };
  62.       image.apply(ImageToPixMap(*this));
  63.     }
  64.   };
  65.   
  66.   ImageWindow image_window(*this,title);
  67.   image_window.handle();
  68. }